package br.com.async.controller.responsible;

import java.util.List;

import br.com.async.application.PersonApplication;
import br.com.async.application.UserApplication;
import br.com.async.controller.auth.UserComponent;
import br.com.async.domain.login.User;
import br.com.async.domain.person.Contact;
import br.com.async.domain.person.Person;
import br.com.async.domain.person.PersonType;
import br.com.async.infrastructure.util.ApplicationContextRegisterCore;
import br.com.async.util.ResponseData;
import br.com.async.view.util.AsyncMessage;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;

@Resource
public class ResponsibleController {

	private final Result result;
	private User user;
	private UserApplication userApp = ApplicationContextRegisterCore.getBean(
			"userApplication", UserApplication.class);
	private PersonApplication personApp = ApplicationContextRegisterCore.getBean("personApplication", PersonApplication.class);
	private UserComponent userComponent;

	public ResponsibleController(Result result, UserComponent userComponent) {
		this.result = result;
		this.userComponent = userComponent;
	}
	
	/**
	 * P�gina principal de Respons�vel 
	 */
	@Path("/responsavel/inicio")
	public void index() {
		userComponent.setUrlHome("responsavel/inicio");
	}

	@Path("/responsible")
	public void responsiblePanel() {
//		result.include("pageResponsible", "ui-btn-active");
//		List<Person> list = personApp.listReponsibles(0, 30).getItems();
//		result.include("list", list);
	}
	
	@Path("/responsaveis")
	public void responsibleList() {
		result.include("pageResponsible", "ui-btn-active");
		ResponseData<Person> rd = personApp.listReponsibles(0, 30);
		
		List<Person> list = rd.getItems();
		result.include("list", list);
		
		result.forwardTo(this).responsiblePanel();
	}
	
	@Get("/responsaveis/busca")
	public void responsibleSearch(String busca, String offset){
		
		result.include("pageResponsible", "ui-btn-active");
		
		ResponseData<Person> rd = personApp.findResponsibleName(busca, Integer.parseInt(offset), 30);
		List<Person> list = rd.getItems();
		result.include("list", list);
		
		result.forwardTo(this).responsiblePanel();
	}
	
	@Post("/responsible/findByCode")
	public void responsibleFindByCode(String code){
		Person p = personApp.findByCode(Integer.parseInt(code));
		System.out.println(p.getName());
		System.out.println(p.getContact().getEmail());
		System.out.println(p.getContact().getPhone());
		
		result.use(Results.json()).from(p,"responsible").serialize();
		
		result.use(Results.nothing());
		
	}
	
	@Path("/novo-responsavel")
	public void responsibleWindow() {

	}

	@Post("/responsible/persist")
//	public void persist(User user,
//			String repassword) {
	public void persist(String name, String email, String phone, String username, String passOne, String passTwo) {
		
//		this.user = user;
//		this.user.getPerson().setPersonType(PersonType.RESPONSIBLE);
//		if (user.getPassword().toLowerCase().equalsIgnoreCase(repassword.toLowerCase())) {
//			try {
//				userApp.persist(user);
//				result.use(Results.json())
//						.from(AsyncMessage.RESPONSIBLE_PERSIST.toString(),
//								"resultMessage").serialize();
//			} catch (Exception e) {
//				e.printStackTrace();
//				result.use(Results.json())
//						.from(AsyncMessage.RESPONSIBLE_PERSIST_ERROR.toString(),
//								"resultMessage").serialize();
//			}
//		}
		
		User u = new User();
		Person p = new Person();
		Contact c = new Contact();
		p.setContact(c);
		u.setPerson(p);
		
		
		u.getPerson().setName(name);
		u.getPerson().getContact().setEmail(email);
		u.getPerson().getContact().setPhone(phone);
		u.setUserName(username);
		u.setPassword(passTwo);
		
		
		this.user = u;
		this.user.getPerson().setPersonType(PersonType.RESPONSIBLE);
		
			try {
				userApp.persist(user);
				result.use(Results.json())
						.from(AsyncMessage.RESPONSIBLE_PERSIST.toString(),
								"resultMessage").serialize();
			} catch (Exception e) {
				e.printStackTrace();
				result.use(Results.json())
						.from(AsyncMessage.RESPONSIBLE_PERSIST_ERROR.toString(),
								"resultMessage").serialize();
			}

		result.use(Results.nothing());
	}
}
